<template>
  <div class="handleList pd-20">
    <el-input v-model="searchCondition"
              placeholder="请输入查询条件"
              clearable
              class="w-200"
              @clear="searchMethod"></el-input>
    <el-button type="primary"
               @click="searchMethod">查 询</el-button>
    <el-table stripe
              border
              :data="tableData"
              style="width: 100%"
              height="730"
              class="mt-20">
      <el-table-column label="序号"
                       type="index"
                       align="center"
                       width="50">
      </el-table-column>
      <el-table-column prop="ip"
                       label="设备IP"
                       align="center"
                       width="120">
      </el-table-column>
      <el-table-column prop="goodsDetailName"
                       label="设备名称"
                       align="center">
      </el-table-column>
      <el-table-column prop="hospitalName"
                       label="所属机构"
                       align="center">
      </el-table-column>
      <el-table-column prop="operaterName"
                       label="使用者"
                       align="center"
                       width="150">
      </el-table-column>
      <el-table-column label="设备状态"
                       align="center"
                       width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.status === 0">正常</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间"
                       align="center"
                       width="150">
        <template slot-scope="scope">
          {{commonFunction.timestampFormat(new Date(scope.row.createTime), 'yyyy-MM-dd hh:mm')}}
        </template>
      </el-table-column>
      <el-table-column label="更新时间"
                       align="center"
                       width="150">
        <template slot-scope="scope">
          {{commonFunction.timestampFormat(new Date(scope.row.time), 'yyyy-MM-dd hh:mm')}}
        </template>
      </el-table-column>
      <el-table-column label="操作"
                       width="200">
        <template slot-scope="scope">
          <el-button type="primary"
                     v-if="scope.row.goodsDetailName !== '平板底座'"
                     @click="openDataDialog(scope.row)">手柄检测数据</el-button>
          <el-button type="danger"
                     @click="handleDel(scope.row)">删 除</el-button>
        </template>
      </el-table-column>
      <infinite-loading slot="append"
                        @infinite="initTableData"
                        :identifier="infiniteId"
                        force-use-infinite-wrapper=".el-table__body-wrapper">
      </infinite-loading>
    </el-table>

    <el-dialog title="手柄检测数据详情"
               :visible.sync="dataDialogVisible"
               width="60%">
      <el-table border
                stripe
                :data="handleDetectData"
                style="width: 100%">
        <el-table-column label="序号"
                         type="index"
                         align="center"
                         width="50">
        </el-table-column>
        <el-table-column label="患者姓名"
                         align="center"
                         min-width="120">
          <template slot-scope="scope">
            <span>{{scope.row.baseDetectDto.memberDto.name}}</span>
          </template>
        </el-table-column>
        <el-table-column label="患者年龄"
                         align="center"
                         min-width="100">
          <template slot-scope="scope">
            {{Math.floor(commonFunction.checkAge(scope.row.baseDetectDto.memberDto.age, scope.row.baseDetectDto.createTime))}}岁
          </template>
        </el-table-column>
        <el-table-column label="患者性别"
                         align="center"
                         width="100">
          <template slot-scope="scope">
            <el-tag type="success"
                    v-if="scope.row.baseDetectDto.memberDto.sex === 2">女</el-tag>
            <el-tag type="success"
                    v-if="scope.row.baseDetectDto.memberDto.sex === 1">男</el-tag>
          </template>
        </el-table-column>
        <el-table-column :formatter="detectName"
                         label="检测项"
                         align="center"
                         min-width="120">
        </el-table-column>
        <el-table-column :formatter="detectionMetrics"
                         label="检测指标"
                         align="center"
                         min-width="120">
        </el-table-column>
        <el-table-column label="检测医生"
                         align="center"
                         width="120">
          <template slot-scope="scope">
            <span>{{scope.row.visitRecordDto.visitDoctorName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="检测时间"
                         align="center"
                         min-width="120">
          <template slot-scope="scope">
            {{commonFunction.timestampFormat(new Date(scope.row.lastUpdateTime), 'yyyy-MM-dd hh:mm')}}
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
  import { interpretPro } from '@/utils/interpret.js'
  export default {
    name: "handleList",
    data () {
      return {
        loading: false,
        dataDialogVisible: false,
        pageIndex: 0,
        pageSize: 20,
        searchCondition: '',
        tableData: [],
        handleDetectData: [],
        infiniteId: 0,
        id: ''
      }
    },
    methods: {
      initTableData ($state) {
        this.state = $state;
        let param = {
          searchCondition: this.searchCondition,
          pageSize: this.pageSize,
          pageIndex: this.pageIndex
        };
        this.post(this.api.handleList.url, param).then((res) => {
          if (res.data.code === '200') {
            if (res.data.object.length) {
              this.pageIndex += 1;
              this.tableData = this.tableData.concat(res.data.object);
              $state.loaded();
            } else {
              $state.complete();
            }
          }
        });
      },
      searchMethod () {
        if (this.searchCondition === '') {
          this.pageIndex = 0;
          this.tableData = [];
          this.infiniteId += 1;
        } else {
          this.pageIndex = 0;
          this.tableData = [];
          this.initTableData(this.state)
        }
      },

      handleDel (row) {
        this.$confirm('确认关闭？')
          .then(_ => {
            this.post(this.api.deleteHandle.url, { id: row.id }).then((res) => {
              if (res.data.code === '200') {
                this.$message.success('删除成功');
                this.searchMethod();
              }
            });
          })
          .catch(_ => { });
      },
      openDataDialog (row) {
        this.dataDialogVisible = true;
        this.post(this.api.handleDetectDetails.url, {
          handleId: row.id
        }).then((res) => {
          if (res.data.code === '200') {
            this.handleDetectData = res.data.object;
          }
        });
      },
      // 检测项
      detectName (row) {
        let deviceId = row.deviceId;
        let report = row.baseDetectDto;
        if (deviceId === 48) {
          let type = parseInt(report['type'])
          if (type > 4) {
            deviceId = type
          }
        }
        let item = interpretPro[deviceId]
        for (let i = 0; i < item.length; i++) {
          let detectName = item[i].name
          if (detectName === '血糖') {
            let detectionTime = report['detectionTime']
            if (detectionTime === '11') {
              return detectName + '(空腹)'
            } else if (detectionTime === '12') {
              return detectName + '(餐后一小时)'
            } else {
              return detectName + '(餐后两小时)'
            }
          }
          return detectName
        }
      },
      // 检测指标
      detectionMetrics (row) {
        let deviceId = row.deviceId;
        let report = row.baseDetectDto;
        if (deviceId === 48) {
          let type = parseInt(report['type'])
          if (type > 4) {
            deviceId = type
          }
        }
        let item = interpretPro[deviceId]
        for (let i = 0; i < item.length; i++) {
          if (report[item[i].pro]) {
            return `${report[item[i].pro]}${item[i].unit}`
          }
        }
        return ''
      }
    }
  }
</script>
<style scoped>
</style>
